package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.MapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * @author ybw
 * @version 1.0
 * @date 2021/12/18 19:53
 */
@Component
public class ArticleUpOrDownListener {
  @Autowired private ApArticleConfigMapper articleConfigMapper;

  @KafkaListener(topics = "down_or_up")
  public void upOrDown(String message) {
    Map<String, String> map = JSON.parseObject(message, Map.class);
    if (MapUtils.isEmpty(map)) {
      return;
    }
    // 获取参数
    String articleId = map.get("article_id");
    String enable = map.get("enable");
    int i = Integer.parseInt(enable);
    LambdaQueryWrapper<ApArticleConfig> qw = new LambdaQueryWrapper<>();
    qw.eq(ApArticleConfig::getArticleId,articleId);
    ApArticleConfig articleConfig = articleConfigMapper.selectOne(qw);
    if (ObjectUtils.isEmpty(articleConfig)) {
      return;
    }
    articleConfig.setIsDown(i != 1);
    articleConfigMapper.updateById(articleConfig);
  }
}
